<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>搜索结果</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
            background: #f5f5f5;
        }

        .container {
            max-width: 1200px;
            margin: 20px auto;
            padding: 0 20px;
        }

        .search-header {
            background: white;
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 20px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }

        .search-info {
            display: flex;
            align-items: center;
            gap: 10px;
            margin-bottom: 15px;
        }

        .keyword {
            color: #ff6b00;
            font-weight: bold;
        }

        .category-tags {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
        }

        .category-tag {
            padding: 6px 12px;
            background: #fff9e6;
            border: 1px solid #ffd700;
            border-radius: 15px;
            color: #333;
            cursor: pointer;
            transition: all 0.3s;
        }

        .category-tag:hover {
            background: #ffd700;
            color: white;
        }

        .category-tag.active {
            background: #ffd700;
            color: white;
        }

        .product-grid {
            display: grid;
            grid-template-columns: repeat(5, 1fr);
            gap: 15px;
            margin-top: 20px;
        }

        .product-card {
            background: white;
            border-radius: 4px;
            overflow: hidden;
            transition: all 0.3s;
            position: relative;
            cursor: pointer;
        }

        .product-type-tag {
            position: absolute;
            top: 8px;
            left: 8px;
            padding: 2px 8px;
            border-radius: 10px;
            font-size: 12px;
            color: white;
            z-index: 1;
        }

        .auction-tag {
            background: rgba(255, 68, 0, 0.8);
        }

        .normal-tag {
            background: rgba(82, 196, 26, 0.8);
        }

        .auction-info {
            font-size: 12px;
            color: #ff4400;
            margin-top: 4px;
        }

        .min-increment {
            color: #666;
            font-size: 12px;
            margin-left: 8px;
        }

        .product-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }

        .product-image {
            width: 100%;
            height: 200px;
            object-fit: cover;
            display: block;
        }

        .product-info {
            padding: 8px;
        }

        .product-name {
            font-size: 14px;
            color: #333;
            margin-bottom: 8px;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
            line-height: 1.3;
            height: 36px;
        }

        .price-info {
            display: flex;
            align-items: baseline;
            gap: 5px;
            margin-bottom: 8px;
        }

        .current-price {
            color: #ff4400;
            font-size: 16px;
            font-weight: bold;
        }

        .original-price {
            color: #999;
            font-size: 12px;
            text-decoration: line-through;
        }

        .seller-info {
            display: flex;
            align-items: center;
            gap: 8px;
            padding: 8px;
            border-top: 1px solid #f0f0f0;
        }

        .seller-avatar {
            width: 24px;
            height: 24px;
            border-radius: 50%;
            object-fit: cover;
        }

        .seller-name {
            font-size: 12px;
            color: #666;
            flex: 1;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .like-count {
            position: absolute;
            top: 8px;
            right: 8px;
            background: rgba(0, 0, 0, 0.5);
            color: white;
            font-size: 12px;
            padding: 2px 6px;
            border-radius: 10px;
            display: flex;
            align-items: center;
            gap: 3px;
        }

        .no-results {
            text-align: center;
            padding: 40px;
            color: #666;
        }

        .back-button {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            padding: 8px 16px;
            background: #f5f5f5;
            border: none;
            border-radius: 4px;
            color: #666;
            cursor: pointer;
            transition: all 0.3s;
            text-decoration: none;
            margin-bottom: 20px;
        }

        .back-button:hover {
            background: #e0e0e0;
        }
    </style>
</head>
<body>
    <div class="container">
        <a href="home.html" class="back-button">
            <i class="fas fa-arrow-left"></i>
            返回首页
        </a>

        <div class="search-header">
            <div class="search-info">
                <span>搜索"</span>
                <span class="keyword" id="searchKeyword"></span>
                <span>"的结果</span>
            </div>
            <div class="category-tags" id="categoryTags">
                <!-- 分类标签将通过JavaScript动态生成 -->
            </div>
        </div>

        <div class="product-grid" id="productGrid">
            <!-- 商品卡片将通过JavaScript动态生成 -->
        </div>
    </div>

    <script>
        // 获取并显示搜索关键词
        function displayKeyword(keyword) {
            $('#searchKeyword').text(keyword || '');
        }

        // 显示搜索结果
        function displayResults(data) {
            const productGrid = $('#productGrid');
            productGrid.empty();
            
            // 显示分类标签
            const categoryTags = $('#categoryTags');
            categoryTags.empty();
            
            // 添加"全部"标签
            categoryTags.append(`<div class="category-tag active">全部</div>`);
            
            // 如果没有搜索结果，显示无结果
            if (!data.products || data.products.length === 0) {
                showNoResults();
                if (data.suggestions) {
                    showSuggestions(data.suggestions);
                }
                return;
            }

            // 显示分类标签
            if (data.categories) {
                data.categories.forEach(result => {
                    const category = result.category;
                    categoryTags.append(`
                        <div class="category-tag" data-path="${result.categoryPath}">${category.name}</div>
                    `);
                });
            }

            // 显示商品列表
            displayProducts(data.products);
        }

        // 显示商品列表
        function displayProducts(products) {
            const productGrid = $('#productGrid');
            productGrid.empty();

            products.forEach(product => {
                const image = product.firstImage || 'img/placeholder-product.png';
                const isAuction = product.isAuction === 1;
                const currentPrice = parseFloat(product.price).toFixed(2);
                const originalPrice = product.originalPrice ? parseFloat(product.originalPrice).toFixed(2) : null;
                const likeCount = product.likeCount || 0;
                
                const productCard = `
                    <div class="product-card" onclick="location.href='${isAuction ? 'auction' : 'product'}-detail.html?id=${isAuction ? product.auctionId : product.id}'">
                        <div class="product-type-tag ${isAuction ? 'auction-tag' : 'normal-tag'}">
                            ${isAuction ? (product.auctionStatus === '1' ? '正在进行' : '即将开始') : '闲置'}
                        </div>
                        <div class="like-count">
                            <i class="fas fa-heart"></i>
                            ${likeCount}
                        </div>
                        <img src="${image}" alt="${product.name}" class="product-image" 
                             onerror="this.src='img/placeholder-product.png'">
                        <div class="product-info">
                            <div class="product-name">${product.name}</div>
                            <div class="price-info">
                                <span class="current-price">¥${currentPrice}</span>
                                ${!isAuction && originalPrice ? `<span class="original-price">¥${originalPrice}</span>` : ''}
                                ${isAuction ? `<span class="min-increment">加价幅度：¥${product.minIncrement || 0}</span>` : ''}
                            </div>
                            ${isAuction ? `
                            <div class="auction-info">
                                ${product.auctionEndTime ? `结束时间：${new Date(product.auctionEndTime).toLocaleString()}` : ''}
                            </div>` : ''}
                        </div>
                        <div class="seller-info">
                            <img class="seller-avatar" 
                                 src="${product.sellerAvatar || 'img/default-avatar.png'}"
                                 alt="卖家头像">
                            <span class="seller-name">${product.sellerNickname || '匿名用户'}</span>
                        </div>
                    </div>
                `;
                productGrid.append(productCard);
            });
        }

        // 绑定分类标签点击事件
        $(document).on('click', '.category-tag', function() {
            $('.category-tag').removeClass('active');
            $(this).addClass('active');
            
            const categoryPath = $(this).data('path');
            if (categoryPath) {
                // 使用选中的分类路径重新查询商品
                $.ajax({
                    url: '/product/filter',
                    type: 'GET',
                    data: { categoryPath: categoryPath },
                    success: function(response) {
                        if (response.state === 1 && response.data) {
                            displayProducts(response.data);
                        } else {
                            showNoResults();
                        }
                    },
                    error: function() {
                        showNoResults();
                    }
                });
            } else {
                // 如果点击"全部"标签，重新加载所有搜索结果
                loadSearchResults();
            }
        });

        // 显示无结果提示
        function showNoResults() {
            $('#productGrid').html(`
                <div class="no-results">
                    <i class="fas fa-search"></i>
                    <p>未找到相关商品</p>
                </div>
            `);
        }

        // 加载搜索结果
        function loadSearchResults() {
            const urlParams = new URLSearchParams(window.location.search);
            const keyword = urlParams.get('keyword');
            
            if (!keyword) {
                showNoResults();
                return;
            }

            // 显示搜索关键词
            displayKeyword(keyword);

            // 调用后端搜索接口
            $.ajax({
                url: '/category/search',
                type: 'GET',
                data: { keyword: keyword },
                success: function(response) {
                    console.log(response);

                    if (response.state === 1) {
                        displayResults(response.data);
                    } else {
                        showNoResults();
                    }
                },
                error: function() {
                    showNoResults();
                }
            });
        }

        // 页面加载完成后执行搜索
        $(document).ready(function() {
            loadSearchResults();
        });
    </script>
</body>
</html> 